<?php

require_once dirname(__FILE__) . '/../lib/teacherGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/teacherGeneratorHelper.class.php';

/**
 * teacher actions.
 *
 * @package    sf_sandbox
 * @subpackage teacher
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class teacherActions extends autoTeacherActions {

    public function executeListHistoryRecords(sfWebRequest $request) {
        $id = $this->getRequestParameter('id');

        $this->redirect('change_logs/listViewDetails?model_name=Teacher&model_id=' . $id);
    }

    public function executeApproveHang() {
        $id = $this->getRequestParameter('intg_identity');
        $teacher = TeacherTable::getInstance()->retrieveByIntgId($id);

        $this->forward404Unless($teacher);

        $teacher->setAsHanged(true);

        $this->getUser()->setFlash('notice', I18nUtilities::__('Teacher hanging has been approved'));
        $this->redirect('teacher/index');
    }

    public function executeStopHang() {
        $id = $this->getRequestParameter('intg_identity');
        $teacher = TeacherTable::getInstance()->retrieveByIntgId($id);

        $this->forward404Unless($teacher);

        $teacher->setHangeStatus2(TeacherTable::HANG_STATUS_REQUEST_STOP_HANGING, true);

        $this->getUser()->setFlash('notice', I18nUtilities::__('Teacher hanging has been stopped'));
        $this->redirect('teacher/index');
    }

    /**
     * override the default buildQuery() method to add filtering by editor sector functionality
     * @return Doctrine_query
     */
    protected function buildQuery() {
        $query = parent::buildQuery();
        if ($this->getUser()->hasCredential('SUPERVISOR_ADMIN')) {
            $sectors = $this->getUser()->getGuardUser()->getSectors();
            $ids = array();
            foreach ($sectors as $one)
                $ids[] = $one->get('intg_id');
            if (is_array($ids) && count($ids) > 0) {
                $query->innerJoin('r.School sch');
                $query->whereIn('sch.sector_intg_id', $ids);
            }
        }

        return $query;
    }

    public function executeExport(sfWebRequest $request) {
        $q = parent::buildQuery();

        $q->select('
            intg_identity
            , mobile
            , CONCAT(f_name," ",s_name," ",t_name," ",l_name) as name
            , s.name as school
            , is_manager'
        );

        $q->leftJoin('r.School s');

        $items = $q->fetchArray();

        $order = array(
            'intg_identity'
            , 'name'
            , 'mobile'
            , 'school'
            , 'is_manager'
        );

        $labels = array(
            'intg_identity' => 'Intg identity'
            , 'name' => 'Name'
            , 'mobile' => 'Mobile'
            , 'is_manager' => 'Is manager'
            , 'school' => 'School'
        );

        $partial = $this->getPartial('export', array('items' => $items, 'order' => $order, 'labels' => $labels));

        $word = globals::wordify($partial);

        header('Content-type: application/doc');
        header('Content-Disposition: attachment; filename="teacher_list_' . (substr(time(), 0, 5)) . '.doc"');
        header('Content-Description: File Transfer');

        die($word);
    }

    public function executeSendCustomSms(sfWebRequest $request) {
        $this->setLayout('popupLayout');

        $this->send_succ = false;

        $q = $this->buildQuery();
        $this->user_count = $q->count();
        if (!$this->user_count)
            return;

        $this->form = new sendSmsForm();


        if ($request->isMethod('POST')) {
            $data = $this->getRequestParameter($this->form->getName());
            $this->form->bind($data);

            if ($this->form->isValid()) {
                $values = $this->form->getValues();

                $q->select('id')->addSelect('mobile');
                $phone_nums = array();

                foreach ($q->fetchArray() as $row) {
                    $phone_nums[] = $row['mobile'];
                }

                globals::send_sms($values['sms_body'], $phone_nums);

                $this->send_succ = true;
            }
        }
    }

}
